package aquarium.items;

import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.net.URL;
import java.util.Collection;


import aquarium.gui.Aquarium;
import aquarium.time.TManager;
import aquarium.util.RandomNumber;

public class Bubble extends MobileItem{	

	private static URL _url = ClassLoader.getSystemResource("image/bubble02.png");
	private static Image _bubbleImage = Toolkit.getDefaultToolkit().createImage(_url);
	private Fish _fish_creator;


	public Bubble(Point position, 
			int size,
			boolean destructible, 
			boolean letal, 
			long move_delay,
			long change_target_delay,
			Point target,
			boolean visitor,
			TManager tmanager) {
		super(_bubbleImage , position, size, destructible, letal, move_delay, change_target_delay, target, visitor, tmanager);
	}

	public void newTarget(Collection<AquariumItem> items){
		Point pt = new Point(RandomNumber.randomPoint(0, Aquarium.SIZE_AQUA_X-Bubble.getMaxWidth(), 0, Aquarium.SIZE_AQUA_Y-Bubble.getMaxWidth()));
		get_target().setLocation(pt); 
	}

	public void init_bubble(AquariumItem fish_killer, AquariumItem fish_victim, int size_x, int size_y){
		this._fish_creator = (Fish)fish_killer;
		Point killer = new Point(fish_killer.get_position());
		Point victim = new Point(fish_victim.get_position());

		this.set_position(killer);
		int x;
		double y;
		
		if(victim.x <= killer.x)
			x = 0;
		else
			x = size_x;
		try{
			double coeff_directeur = (killer.y - victim.y)/(killer.x - victim.x);
			y = victim.y - coeff_directeur * victim.x;
			this.set_target(new Point(x, new Integer((int)y)));
		}catch (ArithmeticException ae) {
			System.out.println("Division par zero !");
		}
	}

	public boolean isFishCreator(Fish fish){
		return (_fish_creator != null && _fish_creator.equals(fish)); // null si le poisson est mort donc il ne peut pas être le createur de la bulle
	}
}
